package com.example.demo.controller;

import com.example.demo.constant.SecurityConstant;
import com.example.demo.entity.Goods;
import com.example.demo.exception.ApplicationException;
import com.example.demo.service.GoodsService;
import com.example.demo.vo.Result;
import com.example.demo.vo.ResultCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.models.security.SecurityScheme;
import jakarta.annotation.Resource;
import lombok.NonNull;
import org.springframework.data.repository.query.Param;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.validation.TypeInfoProvider;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: TODO
 * @Author Mr.Tang
 */
@Tag(name = "商品功能接口")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;


    @Operation(summary = "增加商品信息")
    @PostMapping("/addGoods")
    public Result addGoods(@Parameter(name = "goodsName", description = "商品名", required = true) @NonNull String goodsName,
                           @Parameter(name = "picture", description = "商品图片(可选)") String picture,
                           @Parameter(name = "description", description = "商品描述", required = true) @NonNull String description,
                           @Parameter(name = "price", description = "商品价格", required = true) @NonNull Double price,
                           @Parameter(name = "number", description = "商品库存", required = true) @NonNull Long number,
                           @Parameter(name = "type", description = "商品类型", required = true) @NonNull String type,
                           @Parameter(name = "businessId", description = "商家id", required = true) @NonNull Long businessId,
                           @Parameter(name = "address", description = "发货地址", required = true) @NonNull String address,
                           @Parameter(name = "discountedPrice", description = "商品优惠") Double discountedPrice) {
        // 1. 封装商品对象
        Goods goods = new Goods();
        goods.setGoodsName(goodsName);
        goods.setPicture(picture);
        goods.setDescription(description);
        goods.setPrice(price);
        goods.setNumber(number);
        goods.setType(type);
        goods.setBusinessId(businessId);
        goods.setAddress(address);
        goods.setDiscountedPrice(discountedPrice);

        // 设置时间
        LocalDateTime dateTime = LocalDateTime.now();
        goods.setCreateTime(dateTime);
        goods.setUpdateTime(dateTime);

        // 2. 调用service
        goodsService.addGoods(goods);

        return Result.ok(ResultCode.SUCCESS);
    }

    @Operation(summary = "根据id删除一条记录")
    @DeleteMapping("/deleteGoodsById")
    public Result deleteGoodsById(@Parameter(name = "id", description = "商品id") @NonNull Long id) {
        // 调用service
        goodsService.deleteGoodsById(id);
        // 返回
        return Result.ok(ResultCode.SUCCESS);
    }

    @Operation(summary = "商家根据商品名称查询商品")
    @GetMapping("/getGoodsByNameWithBId")
    public Result<List<Goods>> getGoodsByNameWithBId(@Parameter(name = "goodsName", description = "商品名称", required = true) String goodsName,
                                                     @Parameter(name = "index", description = "排序方式", required = true) @NonNull Integer index,
                                                     @Parameter(name = "Bid", description = "商家id", required = true) @NonNull Long Bid) {
        // 调用service
        List<Goods> list = goodsService.getGoodsByNameWithBId(goodsName, index, Bid);
        // 返回
        return Result.ok(list);
    }








    @Operation(summary = "根据id获取商品详情")
    @GetMapping("/getDescriptionById")
    public Result getDescriptionById(@Parameter(name = "id", description = "商品id") @NonNull Long id) {
        // 调用service
        String description = goodsService.getDescriptionById(id);
        // 成功返回
        return Result.ok(description);
    }

    @Operation(summary = "根据商品id拿到商品信息")
    @GetMapping("/getGoodsById")
    public Result<Goods> getGoodsById(@Parameter(name = "id", description = "商品id") @NonNull Long id) {
        // 调用service
        Goods goods = goodsService.getGoodsById(id);
        // 成功返回
        return Result.ok(goods);
    }







    @Operation(summary = "获取所有商品信息")
    @GetMapping("/getAllGoodsInfo")
    public Result<List<Goods>> getAllGoodsInfo() {
        // 调用service
        List<Goods> list = goodsService.getAllGoodsInfo();

        // 成功返回
        return Result.ok(list);
    }

    @Operation(summary = "根据商品类型获取商品")
    @GetMapping("/getGoodsByType")
    public Result<List<Goods>> getGoodsByType(@Parameter(name = "type", description = "商品类型") @NonNull String type) {
        // 调用service
        List<Goods> list = goodsService.getGoodsByType(type);
        // 成功返回
        return Result.ok(list);
    }


    @Operation(summary = "根据价格范围筛选商品")
    @GetMapping("/getGoodsInPriceRange")
    public Result<List<Goods>> getGoodsInPriceRange(@Parameter(name = "minPrice", description = "最低价格") @NonNull Long minPrice,
                                                    @Parameter(name = "maxPrice", description = "最高价格") @NonNull Long maxPrice,
                                                    @Parameter(name = "goodsType", description = "商品类型") String goodsType) {
        // 调用service
        List<Goods> list = goodsService.getGoodsInPriceRange(minPrice, maxPrice, goodsType);
        // 成功返回
        return Result.ok(list);
    }


    @Operation(summary = "根据价格从高到低排序商品")
    @GetMapping("/getGoodsOrderByPriceDesc")
    public Result<List<Goods>> getGoodsOrderByPriceDesc() {
        // 调用service
        List<Goods> list = goodsService.getGoodsOrderByPriceDesc();

        // 成功返回
        return Result.ok(list);

    }


    @Operation(summary = "根据价格从低到高排序商品")
    @GetMapping("/getGoodsOrderByPriceAsc")
    public Result<List<Goods>> getGoodsOrderByPriceAsc() {
        // 调用service
        List<Goods> list = goodsService.getGoodsOrderByPriceAsc();

        // 成功返回
        return Result.ok(list);
    }



    @Operation(summary = "根据商品名称查询商品")
    @GetMapping("/getGoodsByName")
    public Result<List<Goods>> getGoodsByName(@Parameter(name = "goodsName", description = "商品名称", required = true) String goodsName,
                                              @Parameter(name = "index", description = "排序方式", required = true) Integer index) {
        // 调用service
        List<Goods> list = goodsService.getGoodsByName(goodsName, index);
        // 返回
        return Result.ok(list);
    }



    @Operation(summary = "根据商品id减少库存")
    @PostMapping("/reduceGoodsByGoodsId")
    public Result reduceGoodsByGoodsId(@Parameter(name = "goodsId", description = "商品id") @NonNull Long goodsId,
                                       @Parameter(name = "number", description = "下单数量") @NonNull Long number) {
        // 调用service
        Integer ret = goodsService.reduceGoodsByGoodsId(goodsId, number);

        // 返回
        if (ret == 0) {
            return Result.ok(ResultCode.CLIENT_A0015);
        }
        return Result.ok(ResultCode.SUCCESS);
    }


    @Operation(summary = "根据商家ID查询商品")
    @GetMapping("/getGoodsByBusinessId")
    public Result<List<Goods>> getGoodsByBusinessId(@Parameter(name = "businessId", description = "商家ID", required = true) Long businessId) {
        // 调用service
        List<Goods> list = goodsService.getGoodsByBusinessId(businessId);
        // 返回
        return Result.ok(list);
    }


    @Operation(summary = "更新商品信息")
    @PutMapping("/updateGood")
    public void updateGood(@RequestBody @Validated Goods good){
        goodsService.updateGood(good);
    }



}
